package com.dhcc.android.base.db.Sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.HashMap;
import java.util.Map;

/**
 * 任务包数据库管理
 * 
 * @author zhoudan
 * @date 2015年7月20日
 */
public class TaskDBManager {

    private DBHelper helper;
    private SQLiteDatabase db; 
     
    private String table = "Task";
    
    public TaskDBManager(Context context){
        helper = new DBHelper(context);
        db = helper.getWritableDatabase();
        db.execSQL("CREATE TABLE IF NOT EXISTS "+table+" (taskId VARCHAR PRIMARY KEY  NOT NULL ,"
                + " title VARCHAR, time VARCHAR, taskdate VARCHAR, imageFTP VARCHAR, demandFTP VARCHAR, surveyFTP VARCHAR, taskFTP VARCHAR);");
        
    }
    
    public void addTask(Map<String, String> task){
        db.beginTransaction();
        try{
            String sql = "DELETE FROM "+table+ " where taskId = '"+task.get("taskid")+"'";
            db.execSQL(sql);
            System.out.println("###sql:"+sql);
            sql = "insert into "+table+" values(?, ?, ?, ?, ?, ?, ?, ?)";
            db.execSQL(sql, new Object[]{task.get("taskid"),task.get("title"),task.get("time"),task.get("desc"),task.get("imageftp"),
                    task.get("demandftp"),task.get("dataftp"),task.get("path")});
            
            db.setTransactionSuccessful();
        }finally{
            db.endTransaction();
            
            db.close();
        }
    }
    
    public Map<String, String> getTaskFTPPath(String taskId){
        Map<String, String> map = new HashMap<String, String>();
        
        String sql = "select * from "+table + " where taskId = '"+taskId+"'";
        Cursor c = db.rawQuery(sql, null);
        System.out.println("###sql:"+sql);
        while(c.moveToNext()){
            map.put("imageFTP", c.getString(c.getColumnIndex("imageFTP")));
            map.put("demandFTP", c.getString(c.getColumnIndex("demandFTP")));
            map.put("surveyFTP", c.getString(c.getColumnIndex("surveyFTP")));
        }
        c.close();
        db.close();
        return map;
    }
    
}
